package com.tools.web.interceptor.auth.custom;

import com.tools.common.object.Note;
import com.tools.common.security.secret.TwoWayCypher;
import com.tools.db.cache.local.LocalCache;
import com.tools.web.interceptor.auth.AuthSource;
import com.tools.web.interceptor.auth.AuthToken;
import com.tools.web.interceptor.auth.AuthUserService;
import com.tools.web.interceptor.auth._common.LocalSingleAuthenticationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * 用 【JSON + 本地缓存】 自定义实现的 “用户单在线记录” 的 AuthenticationManager 身份认证拦截器
 * */
@Note("用 【JSON + 本地缓存】 自定义实现的 “用户单在线记录” 的 AuthenticationManager 身份认证拦截器")
public final class JSONLocalSingleAuthenticationManager extends LocalSingleAuthenticationManager {

    @Note("用于双向加密解密 AuthToken 的工具实例")
    private final TwoWayCypher cypher;

    @Note("日志输出对象")
    private static final Logger LOGGER = LoggerFactory.getLogger(JSONLocalSingleAuthenticationManager.class);

    public JSONLocalSingleAuthenticationManager(LocalCache localCache, AuthSource source, TwoWayCypher cypher) {
        this(localCache, source, null, cypher);
    }

    public JSONLocalSingleAuthenticationManager(LocalCache localCache, AuthSource source, AuthUserService authUserService, TwoWayCypher cypher) {
        super(localCache, source, authUserService);
        this.cypher = Objects.requireNonNull(cypher, "自定义实现的 AuthToken 用于双向加密解密的工具 TwoWayCypher 实例不能为 null");
    }

    @Override
    protected Logger protected_getLogger() {
        return LOGGER;
    }

    @Override
    public AuthToken decryptAuthToken(String encryptedToken) {
        return new JSONAuthToken(this.cypher, encryptedToken);
    }

    @Override
    public AuthToken newEmptyAuthToken() {
        return new JSONAuthToken(this.cypher);
    }

    public TwoWayCypher getCypher() {
        return cypher;
    }
}
